﻿Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.Services
Imports DotNetNuke.Entities
Imports System.Net.Mail
Imports System.Web.Security
Imports System.Text

Namespace AboutDNN.DNNMVC

    ''' <summary>
    ''' DNN Helper Interface. Abstracts the concreate implemenation of the dnn helper class
    '''      to aid in thesting and decoupling
    ''' </summary>
    ''' <remarks></remarks>
    Public Interface IDNNHelper
        Function PortalID() As Integer
        Function ModuleID() As Integer
        Function UserID() As Nullable(Of Integer)
        Function PortalGUID() As Guid
        Function HomeUrl() As String
        Function PortalPath() As String
        Function UserIsAdmin() As Boolean
        Function UserIsHost() As Boolean
        Function IsInRole(ByVal roleName As String) As Boolean
        Function UserEmail() As String
        Function TabID() As Integer?
        Function GetUserInfo(ByVal UserId As Integer) As DotNetNuke.Entities.Users.UserInfo
        Function GetUserName(ByVal UserID As Integer) As String
        Function GetClientUrl(ByVal strUrl As String) As String
        Sub LogException(ByVal ex As Exception)
        Function CreateUrl(ByVal ParamArray params() As String) As String
        Function CreateUser(ByVal account As DNNAccount) As DNNUserCreateStatus
        Sub DeleteUser(ByVal UserName As String)
        Sub SendEmail(ByVal msg As MailMessage)
    End Interface



    ''' <summary>
    ''' Concreate implementation of the IDNNHelper interface
    ''' </summary>
    Public Class DNNHelper
        Implements IDNNHelper

        Public Function PortalID() As Integer Implements IDNNHelper.PortalID
            If App.IsAjaxRequest Then
                Return App.GetInt(Params.PortalId)
            Else
                Return DotNetNuke.Common.GetPortalSettings.PortalId
            End If
        End Function

        Public Function ModuleID() As Integer Implements IDNNHelper.ModuleID
            If App.Cache.GetValue(Params.ModuleId) IsNot Nothing Then
                Return App.Cache.GetValue(Params.ModuleId)
            End If

            Return 0
        End Function

        Public Function TabID() As Integer? Implements IDNNHelper.TabID
            If App.IsAjaxRequest Then
                Return App.GetInt(Params.TabId)
            Else
                Return GetPortalSettings.ActiveTab.TabID
            End If
        End Function

        Private Function GetUser() As Users.UserInfo
            If App.MVCContext.IsAuthenticated Then
                Dim key = Params.Cache.User(App.MVCContext.UserName)
                If App.MVCContext.Items(key) Is Nothing Then
                    App.MVCContext(key) = Security.Membership.MembershipProvider.Instance.GetUserByUserName(PortalID, App.MVCContext.UserName, True)
                End If
                Return App.MVCContext(key)
            End If
        End Function

        Public Function UserID() As Nullable(Of Integer) Implements IDNNHelper.UserID
            If App.MVCContext.IsAuthenticated Then
                Return GetUser.UserID
            Else
                Return New Nullable(Of Integer)
            End If
        End Function


        Public Function UserIsAdmin() As Boolean Implements IDNNHelper.UserIsAdmin
            If App.MVCContext.IsAuthenticated Then
                If UserIsHost() OrElse GetUser.IsInRole("Administrators") Then
                    Return True
                End If
            End If
        End Function

        Public Function GetUserInfo(ByVal UserID As Integer) As Users.UserInfo Implements IDNNHelper.GetUserInfo
            Return Users.UserController.GetUser(PortalID, UserID, True)
        End Function

        Public Function GetUserName(ByVal UserID As Integer) As String Implements IDNNHelper.GetUserName
            Return App.DB.SQL.SELECT("DisplayName").FROM("{{{users}}}").WHERE("UserID", UserID).ExecuteScalarString
        End Function

        Public Function UserIsHost() As Boolean Implements IDNNHelper.UserIsHost
            If App.MVCContext.IsAuthenticated Then
                Return GetUser.IsSuperUser
            End If
        End Function

        Public Function IsInRole(ByVal roleName As String) As Boolean Implements IDNNHelper.IsInRole
            If App.MVCContext.IsAuthenticated Then
                Return GetUser.IsInRole(roleName)
            End If
        End Function

        Public Function UserEmail() As String Implements IDNNHelper.UserEmail
            If App.MVCContext.IsAuthenticated Then
                Return GetUser.Email
            End If
        End Function

        Public Function PortalGUID() As Guid Implements IDNNHelper.PortalGUID
            Return GetPortalSettings.GUID
        End Function

        Public Function HomeUrl() As String Implements IDNNHelper.HomeUrl
            Return NavigateURL()
        End Function

        Public Function CreateUrl(ByVal ParamArray params() As String) As String Implements IDNNHelper.CreateUrl
            Return NavigateURL(TabID, "", params)
        End Function

        Public Function GetClientUrl(ByVal strUrl As String) As String Implements IDNNHelper.GetClientUrl
            Return ResolveUrl(strUrl)
        End Function

        Public Sub LogException(ByVal ex As Exception) Implements IDNNHelper.LogException
            Exceptions.LogException(ex)
        End Sub

        Public Function PortalPath() As String Implements IDNNHelper.PortalPath
            Return GetPortalSettings.HomeDirectory
        End Function

        Public Function CreateUser(ByVal account As DNNAccount) As DNNUserCreateStatus Implements IDNNHelper.CreateUser
            Dim newUser = New Users.UserInfo

            newUser.PortalID = PortalID()
            newUser.Username = account.UserName
            newUser.FirstName = account.FirstName
            newUser.LastName = account.LastName
            newUser.DisplayName = account.DisplayName
            newUser.Email = account.Email
            newUser.Membership.Password = account.Password

            If account.Address <> "" And account.City <> "" And account.State <> "" And account.Zip <> "" And account.Country <> "" Then
                newUser.Profile.Street = account.Address
                newUser.Profile.City = account.City
                newUser.Profile.Region = account.State
                newUser.Profile.PostalCode = account.Zip
                newUser.Profile.Country = account.Country
            End If
            If account.Phone <> "" Then
                newUser.Profile.Telephone = account.Phone
            End If
            If account.Fax <> "" Then
                newUser.Profile.Fax = account.Fax
            End If

            newUser.Membership.Approved = True

            Dim status As DNNUserCreateStatus = CInt(Users.UserController.CreateUser(newUser))

            If status = Security.Membership.UserCreateStatus.Success Then
                For Each role In account.AddToRoles
                    Dim rc = New Security.Roles.RoleController
                    Dim ri = rc.GetRoleByName(PortalID, role)
                    If ri IsNot Nothing Then
                        rc.AddUserRole(PortalID, newUser.UserID, ri.RoleID, DateTime.MaxValue)
                    End If
                Next
            End If

            Return status

        End Function

        Public Sub DeleteUser(ByVal UserName As String) Implements IDNNHelper.DeleteUser
            Dim user = Security.Membership.MembershipProvider.Instance.GetUserByUserName(Me.PortalID, UserName, True)
            If user IsNot Nothing Then
                Users.UserController.DeleteUser(user, False, True)
            End If
        End Sub

        Public Sub SendEmail(ByVal msg As MailMessage) Implements IDNNHelper.SendEmail
            Dim client As New SmtpClient
            client.Send(msg)
        End Sub

    End Class



    ''' <summary>
    ''' Concreate implementation of the IDNNHelper interface
    ''' </summary>
    Public Class DNNHelperTestShell
        Implements IDNNHelper


        Public Function PortalID() As Integer Implements IDNNHelper.PortalID
            Return 1
        End Function

        Public Function ModuleID() As Integer Implements IDNNHelper.ModuleID
            Return 1
        End Function

        Public Function TabID() As Integer? Implements IDNNHelper.TabID
            Return 1
        End Function

        Public Function UserID() As Nullable(Of Integer) Implements IDNNHelper.UserID
            If App.MVCContext.IsAuthenticated Then
                Dim key = "UserIDFor:" & App.MVCContext.UserName
                If App.MVCContext(key) Is Nothing Then
                    App.MVCContext(key) = "Admin"
                End If
                Return App.MVCContext(key)
            End If
        End Function

        Public Function PortalGUID() As Guid Implements IDNNHelper.PortalGUID
            Return New Guid("2150E333-8FDC-42A3-9474-1A3956D46DE8")
        End Function

        Public Function HomeUrl() As String Implements IDNNHelper.HomeUrl
            Return App.MVCContext.ApplicationPath & "/TestShell.aspx"
        End Function

        Public Function CreateUrl(ByVal ParamArray params() As String) As String Implements IDNNHelper.CreateUrl
            Dim qs = New StringBuilder
            Dim first = True
            For Each param In params
                If first = False Then
                    qs.Append("&")
                Else
                    first = False
                End If
                qs.Append(param)
            Next
            Return HomeUrl() & "?" & qs.ToString
        End Function

        Public Function GetClientUrl(ByVal strUrl As String) As String Implements IDNNHelper.GetClientUrl
            Return ResolveUrl(strUrl)
        End Function

        Public Sub LogException(ByVal ex As Exception) Implements IDNNHelper.LogException
            Exceptions.LogException(ex)
        End Sub

        Public Function PortalPath() As String Implements IDNNHelper.PortalPath
            Return ApplicationPath & "/portals/" & PortalID()
        End Function

        Public Function CreateUser(ByVal account As DNNAccount) As DNNUserCreateStatus Implements IDNNHelper.CreateUser
            Throw New NotImplementedException
        End Function

        Public Sub SendEmail(ByVal msg As MailMessage) Implements IDNNHelper.SendEmail
            Dim client As New SmtpClient
            client.Send(msg)
        End Sub

        Public Sub DeleteUser(ByVal UserName As String) Implements IDNNHelper.DeleteUser

        End Sub

        Public Function GetUserInfo(ByVal UserId As Integer) As DotNetNuke.Entities.Users.UserInfo Implements IDNNHelper.GetUserInfo

        End Function

        Public Function GetUserName(ByVal UserID As Integer) As String Implements IDNNHelper.GetUserName

        End Function

        Public Function IsInRole(ByVal roleName As String) As Boolean Implements IDNNHelper.IsInRole

        End Function

        Public Function UserEmail() As String Implements IDNNHelper.UserEmail

        End Function

        Public Function UserIsAdmin() As Boolean Implements IDNNHelper.UserIsAdmin

        End Function

        Public Function UserIsHost() As Boolean Implements IDNNHelper.UserIsHost

        End Function
    End Class
End Namespace